package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.dto.leihao.PostShopCartPo;
import com.cskaoyan.shopping.dto.leihao.User;
import com.cskaoyan.shopping.dto.zsz.DeletedProductOfCar;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: leihao
 * @Date: Created in 9:06 2022/7/9
 */
@RestController
public class ProductCartsController {
    @Autowired
    ICartService cartService;
    @GetMapping("/shopping/carts")
    public ResponseData getShoppingCarts(HttpServletRequest servletRequest){
        // 获取user_info请求头
        String userInfoJson = servletRequest.getHeader("user_info");
        // 解析json字符串
        JSONObject userInfoJsonObject = JSON.parseObject(userInfoJson);
        User user = JSON.toJavaObject(userInfoJsonObject, User.class);
        Long userId = user.getUid();

        CartListByIdRequest request = new CartListByIdRequest();
        request.setUserId(userId);
        CartListByIdResponse response = cartService.getCartListById(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(response.getCartProductDtos(), response.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    //添加商品到购物车
    @PostMapping("/shopping/carts")
    public ResponseData postShoppingCarts(@RequestBody PostShopCartPo cartPo){
        AddCartRequest request = new AddCartRequest();
        request.setUserId(cartPo.getUserId());
        request.setItemId(cartPo.getProductId());
        request.setNum(cartPo.getProductNum());
        AddCartResponse response = cartService.addToCart(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(response.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //更新购物车商品数量
    @PutMapping("/shopping/carts")
    public ResponseData putShoppingCarts(@RequestBody PostShopCartPo cartPo){
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(cartPo.getUserId());
        request.setItemId(cartPo.getProductId());
        request.setChecked(cartPo.getChecked());
        request.setNum(cartPo.getProductNum());

        UpdateCartNumResponse response = cartService.updateCartNum(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(response.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @Autowired
    ICartService service;


    /**
     * 删除购物车中的商品
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author little ants
     * @since 2022/07/09 11:45
     */
    @RequestMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") long uid,@PathVariable("pid") long pid) {

        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);

        DeleteCartItemResponse response = service.deleteCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(response.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 删除购物车中选中的商品
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author little ants
     * @since 2022/07/09 11:46
     */
    @RequestMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid") long uid) {

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);

        DeleteCheckedItemResposne resposne = service.deleteCheckedItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(resposne.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(resposne.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(resposne.getMsg());
    }


    /**
     * 全选购物车中的商品
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author little ants
     * @since 2022/07/09 11:46
     */
    @RequestMapping("/shopping/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest request) {
        CheckAllItemResponse response = service.checkAllCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            ResponseData responseData = new ResponseUtil().setData(response.getMsg());
            return responseData;
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
